# coding:utf-8
import json
import sys
import random

from PyQt6.QtCharts import QLineSeries, QChart, QChartView
from PyQt6.QtCore import Qt, QUrl
from PyQt6.QtGui import QIcon, QPainter
from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QLabel, QVBoxLayout, QStackedWidget

from qfluentwidgets import MessageBoxBase, SubtitleLabel, LineEdit, PushButton, setTheme, Theme, SegmentedWidget

class ChartInterface(QWidget):
    def __init__(self, data_name, parent = None):
        super().__init__()
        data_path = "./data/"+data_name
        self.chart = QChart()
        self.chart.setTitle("Line Chart")

        self.series = QLineSeries()
        #根据path，找到json文件，然后找到x，y的两个数组
        index,listx,listy = self.load_data_from_json(data_path)
        for i in range(len(listx)):
            self.series.append(listx[i],listy[i])

        # for i in range(100):
        #     self.series.append(i, random.randint(1, 100))  # 添加随机数据，可根据实际情况修改
        # for x, y in zip(x_data, y_data):
        #     series.append(x, y)

        self.chart.addSeries(self.series)
        self.chart.createDefaultAxes()
        self.chart_view = QChartView(self.chart)
        self.chart_view.setRenderHint(QPainter.RenderHint.Antialiasing)
        self.chart_view.setRubberBand(QChartView.RubberBand.RectangleRubberBand)
        self.chart_view.setInteractive(True)  # 允许交互操作
        self.chart_view.setMouseTracking(True)  # 开启鼠标追踪
        #self.chart_view.setFixedSize(800,300) #改变大小

        layout = QVBoxLayout()
        layout.addWidget(self.chart_view)
        self.setLayout(layout)
        self.setFixedSize(700,330)


    def load_data_from_json(self, filename):
        with open(filename, "r") as json_file:
            data = json.load(json_file)
            index = data["index"]
            listx = data["listx"]
            listy = data["listy"]
            return index, listx, listy
#class ChartInterface2(QWidget):




class ChartMessageBox(MessageBoxBase):
    """ Chart message box """
    def __init__(self,data_path, parent=None):
        super().__init__(parent)
        self.vBoxLayout = QVBoxLayout(self)  # 垂直布局
        self.titleLabel = SubtitleLabel('数据表格', self)#标题
        self.stackedWidget = QStackedWidget(self)
        self.pivot = SegmentedWidget(self)

        # 实例化 ChartInterface 类
        self.chart_interface = ChartInterface(data_path)
        self.ContrastInterface = QLabel('ContrastInterface',self)
        self.OtherInterface = QLabel('OtherInterface',self)

        #add items to pivot
        self.addSubInterface(self.chart_interface, 'ChartInterface', 'Chart')
        self.addSubInterface(self.ContrastInterface, 'ContrastInterface', 'Contrast')
        self.addSubInterface(self.OtherInterface, 'OtherInterface', 'Other')

        # add widget to view layout
        self.viewLayout.addWidget(self.titleLabel)
        self.viewLayout.addLayout(self.vBoxLayout)
        self.vBoxLayout.addWidget(self.pivot)
        self.vBoxLayout.addWidget(self.stackedWidget)
        self.vBoxLayout.setContentsMargins(30, 10, 30, 30)


        # change the text of button
        self.yesButton.setText('保存')
        self.cancelButton.setText('取消')

        self.widget.setMinimumWidth(350)
        self.yesButton.setDisabled(True)

        self.stackedWidget.currentChanged.connect(self.onCurrentIndexChanged)
        self.stackedWidget.setCurrentWidget(self.chart_interface)
        self.pivot.setCurrentItem(self.chart_interface.objectName())

        #self.hideYesButton()
    def onCurrentIndexChanged(self, index):
        widget = self.stackedWidget.widget(index)
        self.pivot.setCurrentItem(widget.objectName())

    def addSubInterface(self, widget, objectName, text):
        widget.setObjectName(objectName)
        #widget.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.stackedWidget.addWidget(widget)
        self.pivot.addItem(
	        routeKey=objectName,
	        text=text,
	        onClick=lambda: self.stackedWidget.setCurrentWidget(widget),
        )
    def _validateUrl(self, text):
        self.yesButton.setEnabled(QUrl(text).isValid())